<?php
/**
 * @Author    紫梦
 * @DateTime  2018-02-09
 * @copyright 响响租车
 * @license   [license]
 * @version   1.0
 * @param     string     $value [description]
 */
namespace app\common\controller;
use app\common\model\Cars;
use app\common\request\ClientLai;
use \think\Db;

class Operate
{
    
    public function unlock($id)
    {
        $config = config('gz_blt');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $model = Db::name('Cars')->where('car_id',$id)->find();
        $data['car_unique_id'] = $model['car_unique_id'];
        $data['seq_id'] = md5(time());
        $data['command'] = "LOCK";
        $data['action'] = 1;
        $data['time'] = time()*1000;
        $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/doCommandAction',http_build_query($data));
        $arr = json_decode($res,true);

        return $arr;
    }

    public function lock($id)
    {
        $config = config('gz_blt');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $model = Cars::get($id);
        $data['car_unique_id'] = $model['car_unique_id'];
        $data['seq_id'] = md5(time());
        $data['command'] = "LOCK";
        $data['action'] = 0;
        $data['time'] = time()*1000;
        $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/doCommandAction',http_build_query($data));
        $arr = json_decode($res,true);

        return $arr;
    }

    public function ignite($id)
    {
        $config = config('gz_blt');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $model = Cars::get($id);
        $data['car_unique_id'] = $model['car_unique_id'];
        $data['seq_id'] = md5(time());
        $data['command'] = "IGNITE";
        $data['action'] = 1;
        $data['time'] = time()*1000;
        $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/doCommandAction',http_build_query($data));
        $arr = json_decode($res,true);

        return $arr;
    }

    public function unignite($id)
    {
        $config = config('gz_blt');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $model = Cars::get($id);
        $data['car_unique_id'] = $model['car_unique_id'];
        $data['seq_id'] = md5(time());
        $data['command'] = "IGNITE";
        $data['action'] = 0;
        $data['time'] = time()*1000;
        $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/doCommandAction',http_build_query($data));
        $arr = json_decode($res,true);

        return $arr;
    }

    public function whistle($id)
    {
        $config = config('gz_blt');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $model = Cars::get($id);
        $data['car_unique_id'] = $model['car_unique_id'];
        $data['seq_id'] = md5(time());
        $data['command'] = "WHISTLE";
        $data['action'] = 1;
        $data['time'] = time()*1000;
        $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/doCommandAction',http_build_query($data));
        $arr = json_decode($res,true);

        return $arr;
    }

    public static function operation($config,$car_unique_id,$instruct,$action,$url)
    {
       
        $obj = new ClientLai($config['user_name'],$config['password']);
     
        $data['car_unique_id'] = $car_unique_id;
        $data['seq_id'] = md5(time());
        $data['command'] = $instruct;
        $data['action'] = $action;
        $data['time'] = time()*1000;
        $res = $obj->request_post($url,http_build_query($data));
        $arr = json_decode($res,true);
        return $arr;
    }
}